rm(list=ls())
gc()
set.seed(63296)
options(scipen=999)

packages <-c("foreign", "ggplot2",  "readstata13", "stargazer", "tidyverse", "broom", 
             "gridExtra")
new.packages <- packages[!(packages %in% installed.packages()[,"Package"])]
if(length(new.packages)) install.packages(new.packages)

lapply(packages, require, character.only = TRUE)
rm(packages)

setwd("PUT YOUR DIRECTORY HERE")

# load data
df <- read.csv("./Datasets/generalizability_dataset_final_panel_imputed.csv")

####################
## Panel analysis ##
####################

# select right variables
data <- df %>% 
  dplyr::select(protest_, SURVEIL, DISAP, KILL, POLPRIS, TORT, autoc, CTRY, YEAR, pop_, event_, imputed) %>% 
  dplyr::filter(CTRY != "Russia")

## impute missings / -999 with sample mean 
data$autoc[data$autoc < 0] <- mean(data$autoc[data$autoc>-1], na.rm=T)
data$DISAP[data$DISAP < 0] <- mean(data$DISAP[data$DISAP>-1], na.rm=T)
data$KILL[data$KILL < 0] <- mean(data$KILL[data$KILL>-1], na.rm=T)
data$POLPRIS[data$POLPRIS < 0] <- mean(data$POLPRIS[data$POLPRIS>-1], na.rm=T)
data$TORT[data$TORT < 0] <- mean(data$TORT[data$TORT>-1], na.rm=T)

# normalize protests (using overall event reporting)
data$PROTESTS <- data$protest_/data$event_

# full model with country and year FEs
model <- lm(PROTESTS ~ SURVEIL + DISAP + KILL + POLPRIS + TORT + autoc + factor(CTRY) + factor(YEAR), data = data)

#summary(model)
tidy(model)


#################
## save as tex ##
#################

stargazer(model,
          dep.var.labels = c("Protest"),
          style = "qje",
          covariate.labels = c("Surveillance", "Disappearances", "Killings", "Political Prisoners", 
                               "Torture", "Autocracy"),
          star.char = c("*", "**", "***"),
          star.cutoffs = c(0.1, 0.05, 0.01),
          omit = c("Constant", "factor"),
          omit.stat = c("rsq", "f", "ser"), 
          omit.table.layout = "n",
          add.lines = list(c("Year fixed effects", "Yes"),
                           c("Locality fixed effects", "Yes")),
          out = "PUT YOUR FILEPATH HERE")




